# @Time    :2025/4/8 20:20
"""
五、Starlette 相关
题目：基于 Starlette 框架和 Uvicorn 服务器，编写一个简单的 Web 应用，
该应用有一个路由 /hello，当访问该路由时，返回一个包含问候语和当前时间的 JSON 响应。
"""
import datetime

from starlette.routing import Route

"""
 通过starlette构建web服务
"""
# import uvicorn
#
# from starlette.applications import Starlette
# from starlette.responses import JSONResponse
#
#
#
# # 定义异步函数来获取随机名言
# async def hello():
#     now=datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
#     params ={"message":"hello,world","time":now}
#     return JSONResponse(params)
# # 创建Starlette应用实例
# app = Starlette(routes=[
#     Route('/hello', hello),
# ])
# if __name__ == "__main__":
#     uvicorn.run(app, host='0.0.0.0', port=8000)


from starlette.applications import Starlette
from starlette.responses import JSONResponse
from starlette.routing import Route
import uvicorn
import datetime


async def hello(request):
    now = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
    result = {"message": "Hello, World!", "time": now}
    return JSONResponse(result)


app = Starlette(debug=True, routes=[
    Route('/hello', hello),
])

if __name__ == "__main__":
    uvicorn.run(app, host='0.0.0.0', port=8000)  # 访问地址：http://192.168.34.28:8000/hello
